﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.Faculty_Member
{
    public partial class Fac_Research : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[3].Selected = true;

        }

        protected void confirm_clicked (object sender, EventArgs e)
        {
            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string id = (string)Session["userName"];

            if(id == null)
                Response.Redirect("../login.aspx");
            try
            {
                DateTime rDate = DateTime.Now;
                string rDateString = rDate.Year + "-" + rDate.Month + "-" + rDate.Day;

                string strSQL = "INSERT INTO Research(about, r_since) VALUES( '" + AboutText.Text + "', NOW() )";
                sqlCommand = new MySqlCommand(strSQL, conn);
                sqlCommand.ExecuteNonQuery();

                strSQL = "SELECT rid FROM Research WHERE about = '" + AboutText.Text + "' ";
                sqlCommand = new MySqlCommand(strSQL, conn);
                readVals = sqlCommand.ExecuteReader();

                readVals.Read();
                int rid = readVals.GetInt32("rid");
                readVals.Close();

                strSQL = "INSERT INTO Researches(fmem_fsid, rid) VALUES( " + id + ", " + rid + " )";
                sqlCommand = new MySqlCommand(strSQL, conn);
                sqlCommand.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                ErrorLabel.Text = msg;
                ErrorLabel.Visible = true;
                goto end;
            }

            ErrorLabel.Text = "Successfully Added!";

            end:
            conn.Close();
        }
    }
}